package net.wanho.controller;

import lombok.RequiredArgsConstructor;
import net.wanho.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * Author：汤小洋
 * Date：2024-04-01 9:32
 * Description：<描述>
 */
@RestController
@RequestMapping("/index")
@RequiredArgsConstructor
public class IndexController {

    // 基于属性注入
    // @Autowired
    // @Resource
    private final ElasticsearchRestTemplate restTemplate;

    // 基于set方法注入
    // @Resource
    // public void setRestTemplate(ElasticsearchRestTemplate restTemplate) {
    //     this.restTemplate = restTemplate;
    // }

    // 基于构造方法注入
    // @Autowired
    // public IndexController(ElasticsearchRestTemplate restTemplate) {
    //     this.restTemplate = restTemplate;
    // }

    /**
     * 创建索引
     */
    @GetMapping("/create")
    public String create(){
        // 创建索引
        IndexOperations indexOperations = restTemplate.indexOps(Student.class);
        indexOperations.create();
        // 创建文档映射
        Document mapping = indexOperations.createMapping(Student.class);
        indexOperations.putMapping(mapping);

        return "success";
    }

    /**
     * 删除索引
     */
    @GetMapping("/delete")
    public String delete(){
        restTemplate.indexOps(Student.class).delete();
        return "success";
    }

}
